package com.hermes.biz.dao.user.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.hermes.biz.common.model.user.UserMessageDO;
import com.hermes.biz.dao.user.UserMessageDAO;

public class UserMessageDAOImpl  extends SqlMapClientDaoSupport implements UserMessageDAO {

	private Logger log = Logger.getLogger(UserMessageDAOImpl.class);
	
	public long getUserNewMessageByType(long userId, int type) {
		
		HashMap<Object, Object> map =new HashMap<Object, Object>();
		map.put("userId", userId);
		map.put("type",type);
		
		try {
			return  (Long) getSqlMapClientTemplate().
					getSqlMapClient().queryForObject("UserMessageDAO.getUserNewMessageByType", map);
		} catch (SQLException e) {
			log.error(e);
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	public List<UserMessageDO> getUserNewMessageByUserId(long userId) {
		try {
			return (List<UserMessageDO>) getSqlMapClientTemplate().
					getSqlMapClient().queryForList("UserMessageDAO.getUserNewMessageByUserId", userId);
		} catch (SQLException e) {
			log.error(e);
		}
		return null;
	}

	public long getUserMessageCountByUserId(long userId) {
		try {
			return  (Long) getSqlMapClientTemplate().getSqlMapClient().queryForObject("UserMessageDAO.getUserMessageCountByUserId", userId);
		} catch (SQLException e) {
			log.error(e);
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	public List<UserMessageDO> getUserMessageByType(long userId, int type) {
		
		HashMap<Object, Object> map =new HashMap<Object, Object>();
		map.put("userId", userId);
		map.put("type",type);
		
		try {
			return (List<UserMessageDO>) getSqlMapClientTemplate().getSqlMapClient().queryForList("UserMessageDAO.getUserMessageByType", map);
		} catch (SQLException e) {
			log.error(e);
		}
		return null;
	}

	public void saveUserMessage(UserMessageDO userMessageDO) {

		try {
			this.getSqlMapClientTemplate().getSqlMapClient().insert("UserMessageDAO.saveUserMessage", userMessageDO);
		} catch (SQLException e) {
			log.error(e);
		}	
	}

}
